package com.yhp.servlet;

import com.yhp.bean.Grade;
import com.yhp.bean.Student;
import com.yhp.service.GradeService;
import com.yhp.service.StudentService;
import com.yhp.service.impl.GradeServiceImpl;
import com.yhp.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.http.HttpClient;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/myedit")
public class EditServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentServiceImpl();
        GradeService gradeService = new GradeServiceImpl();
        //接收参数
        //学生id
        String sid = req.getParameter("stuid");
        int stuid = Integer.parseInt(sid);
        //调用service，得到学生对象
        Student student = studentService.getById(stuid);
        //填充默认值
//        req.setAttribute("stuname",student.getStuname());
//        req.setAttribute("gradename",student.getGrade().getGradeName());
//        req.setAttribute("sex",student.getSex());
//        req.setAttribute("email",student.getEmail());
        List<Grade> gradeList = gradeService.getGradeList();
        System.out.println("班级集合：");
        for (Grade grade : gradeList) {
            System.out.println(grade.getGradeName());
        }
        //传入学生和班级
        req.setAttribute("stu",student);
        req.setAttribute("gradelist",gradeList);
        //跳转
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
}
